package com.lagou.edu.springboothomework.controller;

import com.lagou.edu.springboothomework.entity.Article;
import com.lagou.edu.springboothomework.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ArticleController {

    @Autowired
    private ArticleRepository articleRepository;

    @RequestMapping("index")
    public String index(@RequestParam(name = "pageNum", required = false,defaultValue = "0") Integer pageNum, @RequestParam(name = "pageSize", required = false,defaultValue = "10")Integer pageSize, Model model) {
        Page<Article> page = articleRepository.findAll(PageRequest.of(pageNum, pageSize));
        model.addAttribute("articles", page.getContent());
        model.addAttribute("pageNum", page.getPageable().getPageNumber());
        model.addAttribute("pageSize", page.getPageable().getPageSize());
        model.addAttribute("totalPages", page.getTotalPages());
        return "/client/index";
    }
}
